/**
 * @name MessageInput
 * @author darcrand
 * @desc
 */

import React, { Component } from 'react'

import styles from './styles.module.less'

class MessageInput extends Component {
  static defaultProps = {
    onSend: msg => null
  }

  refText = React.createRef()

  onSubmit = e => {
    const msg = this.refText.current.value
    if ((e.keyCode === 13 || e.wich === 13) && msg.trim()) {
      this.refText.current.value = ''
      this.props.onSend(msg)
    }
  }

  render() {
    return (
      <textarea
        ref={this.refText}
        className={styles.wrap}
        placeholder="hit [enter] to send message"
        autoFocus
        maxLength={200}
        onKeyUp={this.onSubmit}
      ></textarea>
    )
  }
}

export default MessageInput
